<?php

namespace App\Http\Controllers\Index;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Serves\SendSmsServe;
use App\Serves\UserServe;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;


class Login extends Controller
{
    //
    public function index(Request $request){

        var_dump(2222);
        var_dump($request->session()->get('user'));

//            $request->session()->forget('user');
        $cacheCode = Cache::get(config('user.pre_sms_').'18225515455');
        var_dump($cacheCode);
        return view('index.login');
    }

    //发送手机号码，获取验证码
    public function sendSMS(Request $request){
        //模拟发送
        $param = $request->all();

        $res = SendSmsServe::sendSms($param['phone'],'ali');

        if($res){
            return showMsg(200,'短信发送成功');
        }

    }

    //发送验证码登录
    public function check(Request $request){
        $param = $request->all();
        if(empty($param['phone']) || empty($param['code'])){
            return showMsg(0,'参数不合法');
        }
        //先判断验证码是否存在和过期
        $cacheCode = Cache::get(config('user.pre_sms_').$param['phone']);
        if(!$cacheCode || $cacheCode!= $param['code']){
            return showMsg(0,'验证码无效');
        }
        //会员信息处理，若存在这个手机号码则更新user表，不存在则新增，此处只做新增业务演示
        $res = (new UserServe)->doUserCheck( $request, $param['phone']);
        if(!$res){
            return showMsg(0,'登陆失败');
        }
        return showMsg(200,'登陆成功', $res);

    }



}
